import os
import pdb
from docx import Document
import MySQLdb # belongs to mysqlclient
from program.utility import get_docx, get_basic_info, insert_or_update_teacher_info
from program.student_type import dir_names
db = MySQLdb.connect(host ="localhost",user="root", db="student", passwd=os.getenv("db_password"), charset="utf8")
cursor = db.cursor()




def update_teacher_db_from_docx(dir_name, file_name, using_base = True):
    f = open(os.path.join(dir_name, file_name), 'rb')
    d = Document(f)
    for i in d.tables:
        mentor_name = i.cell(3, 2).text.strip().split('（')[0].split(',')[0].split('，')[0].split('(')[0]
        mentor_mobile = i.cell(3, 4).text.strip()
        if len(mentor_name) > 0 and len(mentor_mobile) > 0:
            insert_or_update_teacher_info(cursor, mentor_name, mentor_mobile)
    db.commit()
if __name__ == '__main__':
    for dir_name in dir_names:
        for file_name in get_docx(dir_name):
            update_teacher_db_from_docx(dir_name, file_name)
